﻿using AutoMapper;
using System;
using System.Linq;
using LanhuFx.Services.Model.Dto;
using LanhuFx.Services.Model.View;
using LanhuFx.Identity;
using LanhuFx.Context.PgSql.ResourcesModels;

namespace LanhuFx.Services
{
    public class RoleService
    {
        private readonly LanhuFxContext _Context;
        private readonly IMapper _mapper;
        private readonly TokenService _tokenService;
        public RoleService(LanhuFxContext arg_Context
            , IMapper arg_mapper, TokenService arg_tokenService)
        {
            _Context = arg_Context;
            _tokenService = arg_tokenService;
            _mapper = arg_mapper;
        }

        /// <summary>
        /// 查看角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public View_Role GetRole(Guid id)
        {
            var data = _Context.IdentityRole.FirstOrDefault(c => c.Id == id);
            if (data == null)
            {
                throw new ResponseException(ResponseStateCdode.error, string.Format("该角色信息不存在"));
            }
            return _mapper.Map<View_Role>(data);
        }

        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public string CreateRole(Dto_CreateRole arg)
        {
            if (_Context.IdentityRole.Where(o => o.Name == arg.Name).Any())
            {
                throw new ResponseException(ResponseStateCdode.error, string.Format("角色名称重复"));
            }

            var item = _mapper.Map<IdentityRole>(arg);
            item.Teamid = _tokenService.GetToken().TeamId;
            _Context.IdentityRole.Add(item);
            _Context.SaveChanges();
            return "添加成功";
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteRole(Guid id)
        {
            var data = _Context.IdentityRole.FirstOrDefault(c => c.Id == id);
            if (data == null)
            {
                throw new ResponseException(ResponseStateCdode.error, string.Format("该角色信息不存在"));
            }
            _Context.IdentityRole.Remove(data);
            _Context.SaveChanges();
            return "删除成功";
        }

        /// <summary>
        /// 修改角色名称
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public string EditRole(Dto_EditRole arg)
        {
            var role = _Context.IdentityRole.FirstOrDefault(c => c.Id == arg.Id);
            if (role == null)
            {
                throw new ResponseException(ResponseStateCdode.error, string.Format("该角色信息不存在"));
            }
            if (role != null)
            {
                if(_Context.IdentityRole.Where(o =>o.Id != arg.Id && o.Name == arg.Name).Any())
                {
                    throw new ResponseException(ResponseStateCdode.error, string.Format("角色名称重复"));
                }

                role.Name = arg.Name;
            }
            _Context.SaveChanges();
            return "修改成功";
        }

    }
}
